﻿using MvvmCross.Wpf.Platform;
using MvvmCross.Wpf.Views;
using System.Windows.Threading;

namespace Sidvall.UI.Wpf
{
    public class TestApplicationManager : Sidvall.UI.Wpf.ApplicationManager
    {
        #region Public Members

        #region CreateSetup

        protected override MvxWpfSetup CreateSetup(Dispatcher uiThreadDispatcher, IMvxWpfViewPresenter presenter)
        {
            return new Setup(uiThreadDispatcher, presenter);
        }

        #endregion
        #region OnInitialized

        protected override void OnInitialized()
        {
            var windows = (MainWindow)Sidvall.UI.Wpf.SystemContext.Current.MainWindow;
            var model = new Sidvall.UI.Wpf.ViewModels.MainWindowViewModel();
            model.Start("Test Application");
            windows.ViewModel = model;
        }

        #endregion
        #region RegisterTypes

        protected override void RegisterTypes()
        {
            // Framework
            Sidvall.SystemContext.Current = new Sidvall.SystemContext()
            {
                AuthorizationManager = new Sidvall.Security.AuthorizationManager(new Sidvall.Security.PublicAuthorizationProvider()),
                AuthenticationManager = new Sidvall.Security.AuthenticationManager(new Sidvall.Security.AuthenticationProvider()),
                CacheManager = new Sidvall.Caching.CacheManager(new Sidvall.Caching.MemoryCache()),
                ClaimManager = new Sidvall.Security.ClaimManager(new Sidvall.Security.ClaimsPrincipalManager()),
                CodeManager = new Sidvall.Reflection.CodeManager(new Sidvall.Reflection.Code()),
                LauncherManager = new Sidvall.SystemEnvironment.LauncherManager(new Sidvall.SystemEnvironment.Windows.Launcher()),
                UserExceptionManager = new Sidvall.ExceptionHandling.ExceptionManager(new Sidvall.ExceptionHandling.UserExceptionProvider()),
                SystemExceptionManager = new Sidvall.ExceptionHandling.ExceptionManager(new Sidvall.ExceptionHandling.SystemExceptionProvider()),
                HttpClientFactory = new Sidvall.Net.HttpClientFactory(),
                //LogManager = new Sidvall.Logging.LogManager(new Sidvall.Logging.EL.Log()),
                PrincipalContextManager = new Sidvall.Security.PrincipalContextManager(new Sidvall.Security.PrincipalContextProvider()),
                PrincipalManager = new Sidvall.Security.PrincipalManager(new Sidvall.Security.PrincipalProvider()),
                SmtpClientManager = new Sidvall.Net.Mail.SmtpClientManager(new Sidvall.Net.Mail.SmtpClient()),
                SoapClientFactory = new Sidvall.Net.SoapClientFactory(),
                SystemStorageManager = new Sidvall.IO.StorageManager(new Sidvall.IO.FileSystem()),
                TransactionScopeFactory = new Sidvall.Data.TransactionScopeFactory(),
                TypeCache = new Sidvall.Reflection.TypeCache(),
                UserManager = new Sidvall.Security.UserManager(new Sidvall.Security.PublicAuthorizationProvider()),
                UserStorageManager = new Sidvall.IO.StorageManager(new Sidvall.IO.FileSystem(Sidvall.IO.FolderType.LocalApplicationData)),

                // Client specific
                CultureManager = new Sidvall.Globalization.CultureManager(new Sidvall.UI.Wpf.Globalization.CultureProvider()),
                ResourceManager = new Sidvall.Resources.ResourceManager(new Sidvall.UI.Wpf.Resources.ResourceProvider()),
                SettingsManager = new Sidvall.Configuration.SettingsManager(new Sidvall.UI.Wpf.Configuration.ApplicationSettings()),
            };
            Sidvall.UI.SystemContext.Current = new Sidvall.UI.SystemContext()
            {
                ApplicationManager = new Sidvall.UI.ApplicationManager(),
                ConfigurationContext = new Sidvall.UI.Configuration.ConfigurationContext(new Sidvall.UI.Wpf.Views.ContentControlRegionManager()),
                DialogManager = new Sidvall.UI.Dialogs.DialogManager(
                    new Sidvall.UI.Wpf.Dialogs.FileDialog(),
                    new Sidvall.UI.Wpf.Dialogs.FolderDialog(),
                    new Sidvall.UI.Wpf.Dialogs.XamMessageDialogManager(),
                    new Sidvall.UI.Wpf.Dialogs.MessageBoxDialog(),
                    new Sidvall.UI.Wpf.Dialogs.SecurityDialogManager(),
                    new Sidvall.UI.Wpf.Dialogs.ProgressDialogManager(),
                    new Sidvall.UI.Wpf.Dialogs.WebDialogManager()),
                NavigationManager = new Sidvall.UI.Navigation.NavigationManager(),
            };

            // Application specific
            Sidvall.UI.Wpf.ViewModels.SystemContext.Current.FileDialog1 = new Sidvall.UI.Wpf.Dialogs.FileDialog()
            {
            };
            Sidvall.UI.Wpf.ViewModels.SystemContext.Current.FileDialog2 = new Sidvall.UI.Wpf.Dialogs.XamFileDialogManager()
            {
                RootPath = @"C:\System",
            };
        }

        #endregion

        #endregion
        #region Factory Members

        #region Current

        private static readonly object _Mutex = new object();
        private static volatile TestApplicationManager _Current;
        public static TestApplicationManager Current
        {
            get
            {
                if (_Current == null)
                {
                    lock (_Mutex)
                    {
                        if (_Current == null)
                        {
                            var item = new TestApplicationManager();
                            _Current = item;
                        }
                    }
                }
                return _Current;
            }
            set
            {
                lock (_Mutex)
                {
                    _Current = value;
                }
            }
        }

        #endregion

        #endregion
    }
}
